use strict;
use warnings;
use Data::Dumper;
use Benchmark qw( cmpthese ) ;

my @masas_aas;
use Core::IntConsts qw(get_masas_aa get_masas_mod);
my %masas_aa = %{&get_masas_aa};
my %masas_mod = %{&get_masas_mod};




my $z = 1;

#Integration 375
sub calculate_mz{
	my $pep = "L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR";
	#my $pep = $_[0];
	#my $z = $_[1];
	my $nt;
	my @aas;
	my @mods;

    while($pep =~ /\w/){
		if($pep =~ s/^(\w)\((\w+),(\w+)\)//){
            push @aas, $1;
			push @aas, 'Not';
            push @mods, $2;
			push @mods, $3;
		}
		elsif($pep =~ s/^(\w)\((\w+)\)//){
            push @aas, $1;
            push @mods, $2;
		}
		else{
			$pep =~ s/^(\w)//;
			push @aas, $1;
			push @mods, 'Not';
		}
	}
	my $mass = 18.010565;      #H2O
	for (0..$#aas){
		$mass = $mass + $masas_mod{$mods[$_]} + $masas_aa{$aas[$_]};
	}

	return ($mass + $z * 1.007825) / $z;
}


#New 114% faster, Integration 375
sub calculate_mz_2{
	my $pep = "L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR";
	#my $pep = $_[0];
	#my $z = $_[1];
	my $nt;
	my $mass = 18.010565;      #H2O

    while($pep){
		if($pep =~ s/^(\w)\((\w+),(\w+)\)//){
            $mass +=   $masas_aa{$1} + $masas_mod{$2} + $masas_mod{$3};
		}
		elsif($pep =~ s/^(\w)\((\w+)\)//){
            $mass +=   $masas_aa{$1} + $masas_mod{$2};
		}
		else{
			$pep =~ s/^(\w)//;
			$mass +=   $masas_aa{$1};
		}
	}

	return ($mass + $z * 1.007825) / $z;
}

#"L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR" => '2892.32435',
print Dumper calculate_mz('L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR', 1);
print Dumper calculate_mz_2('L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR', 1);
#
#__END__

cmpthese(100000, {
        a => \&calculate_mz,
        b => \&calculate_mz_2,
        #c => \&pep,
        #d => \&pep
        } );



1;
